package InsLoginRegister;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;

@WebServlet(name = "InsLogin",urlPatterns = "/insLogin")
public class InsLogin extends HttpServlet {
    java.sql.Connection conn = null;
    java.sql.Statement sqlStmt = null;
    java.sql.Statement sqlStmt1= null;
    ResultSet sqlRst = null;
    ResultSet SqlRst1 = null;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
          try {
              Class.forName("org.h2.Driver");
              conn = java.sql.DriverManager.getConnection("jdbc:h2:~/jpa-demo-db;MODE=MYSQL;DB_CLOSE_DELAY=-1", "sa", "sa");
              sqlStmt = conn.createStatement();
              sqlStmt1= conn.createStatement();
              int i = 0;
              if(request.getParameter("submit")!=null) {
                  String MNO = request.getParameter("mno");
                  String PASSWORD = request.getParameter("password");
                  String sql = "select * from MANAGER WHERE MNO = "+"'"+MNO+"'";
                  managerBean mb = new managerBean();
                  sqlRst = sqlStmt.executeQuery(sql);
                  String pno =null;
                  if (sqlRst.next()) {
                      if (PASSWORD.equals(sqlRst.getString("PASSWORD"))) {
                          mb.setMNO(sqlRst.getString("MNO"));
                          request.getSession().setAttribute("mno",sqlRst.getString("MNO"));
                          mb.setNAME(sqlRst.getString("NAME"));
                          mb.setPNO(sqlRst.getString("PNO"));
                          pno =  sqlRst.getString("PNO");
                          mb.setPASSWORD(sqlRst.getString("PASSWORD"));
                          mb.setBIRTHDAY(sqlRst.getString("BIRTHDAY"));
                          mb.setPHONE(sqlRst.getString("PHONE"));
                          i=1;
                      }
                  }
                  String sql1 = "select name from part where pno="+"'"+pno+"'";
                  SqlRst1 = sqlStmt1.executeQuery(sql1);
                  while(SqlRst1.next()) mb.setPosition(SqlRst1.getString("NAME"));
                  response.getWriter().write("登陆成功！");
                  request.getSession().setAttribute("mb", mb);
                  response.getWriter().write("添加成功！");
                 if(i==1) response.sendRedirect("http://localhost:8084/html/index.jsp");
                 else response.sendRedirect("http://localhost:8084/html/error.html");
              }
          }
          catch (Exception ex)
          {
              response.getWriter().write(ex.toString());
          }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
